package bean;

import java.io.Serializable;

import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;


import Util.DAO;

@ManagedBean
@SessionScoped
public class BeanDangNhap implements Serializable{
	private String ten;
	private String matkhau;
	private Boolean dkdn = false;
	public String getTen() {
		return ten;
	}
	public void setTen(String ten) {
		this.ten = ten;
	}
	public String getMatkhau() {
		return matkhau;
	}
	public void setMatkhau(String matkhau) {
		this.matkhau = matkhau;
	}
	
	public String kiemtradangnhap(){
		if(matkhau.length() == 0){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Lỗi Đăng Nhập", "Tên hoặc Mật Khẩu không đúng!"));
			return null;
		}
		int quyen = DAO.kiemtranguoidung(ten, matkhau);
		if(quyen == 1){
			dkdn = true;
			return "admin?faces-redirect=true&amp";
		}
		else{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Lỗi Đăng Nhập", "Tên hoặc Mật Khẩu không đúng!"));
		return null;
		}
	}
	public void checkLogin(ComponentSystemEvent event) {   
		   if (!dkdn) {
		      FacesContext context = FacesContext.getCurrentInstance();
		      ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler)
		         context.getApplication().getNavigationHandler();
		      handler.performNavigation("dangnhap");
		   } 
		}   
}
